package com.example.bookserver.controller;

import com.example.TXTServer.bean.Books;
import com.example.TXTServer.bean.Comment;
import com.example.TXTServer.bean.User;
import com.example.TXTServer.dao.CommentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/TXTApp")
public class CommentController {
    @Autowired
    CommentDao commentDao;
    Map<String, Object> map = new HashMap<>();
    //list
    @ResponseBody
    @RequestMapping("/comment/list")
    public Map<String,Object>bookList(){
        List<Comment>list=commentDao.findAll();
        if (list!=null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",list);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    //add
    @RequestMapping("/comment/add")
    @ResponseBody
    public Map<String,Object> addComment(@RequestBody Comment comment) {
        Comment comment1 = commentDao.saveAndFlush(comment);
        if (comment1 != null) {
            map.put("success", true);
            map.put("message", "成功");
            map.put("data", comment1);
        } else {
            map.put("success", false);
            map.put("message", "失败");
        }
        return map;
    }
    //id
    @RequestMapping("/comment/{id}")
    @ResponseBody
    public Map bookById(@PathVariable Integer id) {
        Optional<Comment> find = commentDao.findById(id);
        if (find != null) {
            map.put("success", true);
            map.put("message", "成功");
            map.put("data", find);
        } else {
            map.put("success", false);
            map.put("message", "失败");
        }
        return map;
    }
    //userid
    @RequestMapping("/comment/bookById")
    @ResponseBody
    public Map bookById(@RequestBody User user) {
        Optional<Comment> find = commentDao.findById(user.getId());
        if (find != null) {
            map.put("success", true);
            map.put("message", "成功");
            map.put("data", find);
        } else {
            map.put("success", false);
            map.put("message", "失败");
        }
        return map;
    }
    //bookid
    @RequestMapping("/comment/userById")
    @ResponseBody
    public Map bookById(@RequestBody Books books) {
        Optional<Comment> find = commentDao.findById(books.getId());
        if (find != null) {
            map.put("success", true);
            map.put("message", "成功");
            map.put("data", find);
        } else {
            map.put("success", false);
            map.put("message", "失败");
        }
        return map;
    }
}
